<?php


class TeacherDBAccess extends DBAccess{
    //put your code here
    private $idTeacherGenerated;
   
    
    public function _construct(){
        parent::__construct();
    }   
    
    
     
    public function validate($Teacher,$registered)
    {
        $errormsg = "";
        $name=$Teacher->getTeacherName();
        $level=$Teacher->getLevels();
        
        //Check if teacher is already in database; Check empty fields
        if($name)
        {
            $teacherList=mysql_query("SELECT * FROM teacher WHERE teacherName='$name'") or 
                die('Could not connect3: ' . mysql_error());
                                            
            if(mysql_num_rows($teacherList) > 0&&!$registered)
            {
                $errormsg = "Teacher is already registered"; 
            }
        }
        else  
            $errormsg = "Please fill up all text fields";
        
        //Check empty fields
        if($level[0]){}
        else  
            if ($errormsg) //If there is already an error, add next error  
                $errormsg = $errormsg . " & no level is selected for the teacher";  
            else  
                $errormsg = "No level is selected for the teacher"; 
                                            
        return $errormsg;
    }
    
    
    
     public function addtoDB($teacher){
           
            //Insert teacher to Database
            $insertTeacherQuery=mysql_query("INSERT INTO teacher(teacherName)
            VALUES ('".$teacher->getTeacherName()."')") or die('Could not connect: ' . mysql_error());
           
           //Get id generated by insert query
            $this->idTeacherGenerated= mysql_insert_id();
       
           //insert levels advised
            
            for($counter=0;$counter<sizeof($teacher->getLevels());$counter++){
            
                    if($teacher->getIndividualLevels($counter)==""){
                                break;
                    }
                    
                    $insertTeacherLevelQuery=mysql_query("INSERT INTO teacheryearlvl (Teacher_idTeacher, Level) VALUES (".$this->idTeacherGenerated.",'".$teacher->getIndividualLevels($counter)."')");
        
            
            }
           
           //check if queries are successful
           if($insertTeacherQuery&&$insertTeacherLevelQuery){
              
           }
           
           else{
               die('Could not connect: ' . mysql_error());
           }
     }
     
     
     
     public function viewDB() {
        
               //Select all teachers in database
                $teacherList=mysql_query("SELECT * FROM teacher") or  die('Could not connect: ' . mysql_error());
                
                return $teacherList;

     }
        
        
     public function delete($idTeacher) {
          
          //delete levels advised
          $deleteTeacherLevelQuery= mysql_query("DELETE FROM teacheryearlvl WHERE Teacher_idTeacher='".$idTeacher."'");
    
          //set teachers handling a subject to none
          $deleteTeacherToSubjectQuery=mysql_query("UPDATE teacher_has_subject SET idTeacher=54 WHERE idTeacher='".$idTeacher."'") ;
    
          //delete teacher
          $deleteTeacherQuery=mysql_query("DELETE FROM teacher WHERE idTeacher='".$idTeacher."'") ;
          
          //check if queries are successful
          if($deleteTeacherLevelQuery&&$deleteTeacherToSubjectQuery&&$deleteTeacherQuery){
              
          }
          
          else{
             die('Could not connect: ' . mysql_error());
          }
    
       }
        
      
      public function searchDB($string) {
          
          //search for teacher
          $searchTeacherQuery= mysql_query("SELECT * FROM teacher WHERE teacherName LIKE '%$string%'");
          
          return $searchTeacherQuery;
      }
      
      
      
       public function getCountInDB($string){
           
           //get number of teachers returned by searchDB
            $countResultsQuery=mysql_query("SELECT count(*) AS 'num' FROM (SELECT * FROM teacher WHERE teacherName LIKE '%$string%' AND teacherName!='NONE') t");
       
            return $countResultsQuery;
            
       }
                                          
        
        
        public function updateDB($Teacher) {
              
              //update teacher
              $updateTeacherQuery=mysql_query("UPDATE teacher SET teacherName='".$Teacher->getTeacherName()."'  WHERE idTeacher =".$Teacher->getIdTeacher() );

              //delete levels advised
              $deleteExistingLevelsQuery= mysql_query("DELETE FROM teacheryearlvl WHERE Teacher_idTeacher='".$Teacher->getIdTeacher()."'");
    
              //replace levels deleted
              for($counter=0;$counter<sizeof($Teacher->getLevels());$counter++){
                     if($Teacher->getIndividualLevels($counter)==""){
                           break;
                     }
            
                     $insertTeacherLevelQuery=mysql_query("INSERT INTO teacheryearlvl (Teacher_idTeacher, Level) VALUES (".$Teacher->getIdTeacher().",'".$Teacher->getIndividualLevels($counter)."')");
        
              }
              
              //check if queries are successful
              if( $updateTeacherQuery&&$deleteExistingLevelsQuery&&$insertTeacherLevelQuery)
              {}   
                
              else{
                  die('Could not connect: ' . mysql_error());
              }
           
         }
         
         
        public function viewEditTeacher($idTeacher){
             
               //returns teacher
               $teacher=mysql_query("SELECT * FROM teacher WHERE idTeacher=".$idTeacher."") or die('Could not connect: ' . mysql_error());
               
               $row = mysql_fetch_array($teacher);
               
               return $row;
                                     
       }
       
       
       
       public function getTeacherFromDB($idSubject){
           //get the teacher from DB handling a particular subject
           $teacher=mysql_query("SELECT * FROM Teacher t,teacher_has_subject s WHERE s.idTeacher=t.idTeacher and s.idSubject=".$idSubject);
           $teacherRow=mysql_fetch_array($teacher);
           
           return $teacherRow;
      }
      
      public function getTeacherLevelsFromDB($idTeacher){
           //get the levels advised by a teacher
           $teacherlevel=mysql_query("SELECT Level FROM teacheryearlvl where Teacher_idTeacher='".$idTeacher."'") or 
                                            die('Could not connect: ' . mysql_error());
           
           return $teacherlevel;
      }
              
      
      public function getSubjectsofTeacher($idTeacher){
          //get subjects handled by a teacher
           $subjects=mysql_query("SELECT * FROM teacher_has_subject WHERE idTeacher=".$idTeacher."") or die('Could not connect: ' . mysql_error());
           
           return $subjects;
      }
}

?>
